/**
 * 表单验证
 * 
 * @param {Object} rules 验证字段的规则
 * @param {Object} messages 验证字段的提示信息
 * 
 */

import basic from './basic.js';
var actmodel = new basic();
actmodel.do = "pro_class"
actmodel.apiall = function () {
  var app = getApp();
  var data = { "op": "apiall" }
  var that= this;
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/' + actmodel.do,
      'data': data,
      'method': "get",
      showLoading: false,
      success: function (res) {
        if (res.data.errno == 0) {
          var reslut= res.data.data;
          if(reslut.status==1){
            if(reslut.obj.level=="2"){
              reslut.obj["tree"] =that.totree(reslut.obj.modellist,2)
            }
          }         
          resolve(res.data.data);
        }
        reject(res.data.message);
      },
      fail: function (res) {
        reject(((res.data && res.data.message) ? res.data.message : res.errMsg));
      },
      complete: function () {
      }
    });
  });
}
actmodel.totree = function (modellist) {
  var trees = [];
  var children = {};
  modellist.forEach(item => {
    if (item["pid"] == 0) {
      trees.push(item);
    }
    else {
      if (!children[item.pid]) {
        children[item.pid] = [];
      }
      children[item.pid].push(item)
    }
  })
  var len = trees.length;
  if (len > 0) {
    for (var i = 0; i < len; i++) {
      if (children[trees[i].id]) {
        trees[i]["children"] = children[trees[i].id]
      }
      else {
        trees[i]["children"] = []
      }
    }
  }
  return trees;
}
module.exports = actmodel;